const webpack = require('webpack'),
  path = require('path'),
  HtmlWebpackPlugin = require('html-webpack-plugin'),
  entryPath = path.join(__dirname, './src/public/'),
  ExtractTextPlugin = require('extract-text-webpack-plugin'),
  UglifyJsPlugin = webpack.optimize.UglifyJsPlugin,
  outputPath = path.join(__dirname, './build/public/'),
  bowerPath = path.join(__dirname, './src/lib/'),
  LiveReloadPlugin = require('webpack-livereload-plugin'),
  ChunkManifestPlugin = require('chunk-manifest-webpack-plugin');

module.exports = {
  entry: {
    initThumb: entryPath + 'scripts/initThumb',
    initStar: entryPath + 'scripts/initStar',
    'vendor': [/*'jQuery', */'xtag'/*, 'axios', 'lodash'*/]  // 使用DNS库上资源jQuery、'axios', 'lodash'；xtag有bug，使用本地
    // thumbTpl: './src/views/index.js'
    // myMath: entryPath + '/myMath'
  },
  output: {
    publicPath: '/',
    path: outputPath,
    filename: 'scripts/[name].[hash:6].js'
  },
  module: {
    /*rules: [{
      test: /\.js$/,
      use: [{
        loader: 'jshint-loader'
      }]
    }]*/
    loaders: [{
      test: /\.js$/,
      loader: 'babel-loader',
      query: {
        presets: ['es2015', "stage-0"]
      }
    }, {
      test: /\.less$/,
      loader: ExtractTextPlugin.extract({
        fallback: "style-loader",
        use: ["css-loader", "less-loader"]
      }) //["style-loader", "css-loader", "less-loader"]
    }]
  },
  plugins: [
    // 提公用js到common.js文件中
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      /*minify: {
        collapseWhitespace: true
      },*/
      filename: 'scripts/[name].[hash:6].bundle.js'
    }),
    //使用ProvidePlugin加载使用频率高的模块
    new webpack.ProvidePlugin({
      $: "zepto-webpack"
    }),
    // js压缩
    /*new UglifyJsPlugin({
      compress: {
        warnings: false
      }
    }),*/
    new LiveReloadPlugin({
      appendScriptsTag: true
    }),
    new ExtractTextPlugin('styles/[name].[hash:6].css'),
    new HtmlWebpackPlugin({
      filename: path.join(outputPath, '..', 'views/layout.html'),
      // template: path.join(entryPath,'..', 'views/layout.html'),
      template: './src/views/layout.html',
      inject: false,
      minify: {
        removeComments: true,
        collapseWhitespace: true
      }
    }),
    new HtmlWebpackPlugin({
      filename: path.join(outputPath, '..', 'views/index.html'),
      templateContent:  require(path.join(entryPath,'..', 'views/index.js')),
      inject: false,
      minify: {
        removeComments: true,
        collapseWhitespace: true
      }
    }),
    new HtmlWebpackPlugin({
      filename: path.join(outputPath, '..', 'views/thumb.html'),
      // template:  path.join(entryPath,'..', '/views/index.js'),
      templateContent:  require(path.join(entryPath,'..', 'views/thumb.js')),
      inject: false,
      chunks: ['initThumb', 'vendor'],
      minify: {
        removeComments: true,
        collapseWhitespace: true
      }
    }),
    new HtmlWebpackPlugin({
      filename: path.join(outputPath, '..', 'views/star.html'),
      // template:  path.join(entryPath,'..', '/views/index.js'),
      templateContent:  require(path.join(entryPath,'..', 'views/star.js')),
      inject: false,
      chunks: ['initStar', 'vendor'],
      minify: {
        removeComments: true,
        collapseWhitespace: true
      }
    }),
    new ChunkManifestPlugin({
      filename: "manifest.json",
      manifestVariable: "webpackManifest"
    }),
  ],
  resolve: {
    extensions: ['.js', '.es', '.less', '.css'],
    alias: {
      // jQuery: bowerPath + 'jQuery/dist/jquery.min.js',
      // systemjs: bowerPath + 'system.js/dist/system.js',
      xtag: bowerPath + 'x-tag-core/dist/x-tag-core.js',
      // axios: bowerPath + 'axios/dist/axios.min.js',
      // lodash: bowerPath + 'lodash/dist/lodash.min.js'
    }
  }
};